package com.witsight.cassandra.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.util.Assert;

import com.witsight.cassandra.bean.NameQuery;
import com.witsight.cassandra.util.CqlBuilder;
import com.witsight.platform.model.BoEntity;
import com.witsight.platform.util.lang.StringUtil;

/**   
 * 说明：CassandraQuery 仓库
 * @Title: CassandraQueryRepon.java 
 * @Package com.witsight.cassandra.dao 
 * @See: {@link NameQueriesRepository}
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年9月21日 上午8:20:33 
 * @version: V1.0
 *
 */
public class NameQueriesRepository implements NamedQueries{
	private static final Logger logger = LoggerFactory.getLogger(NameQueriesRepository.class);
	// 配置仓库
	private final BoEntity configMappings;
	
	/**
	 * 
	 * 说明：构造函数 
	 * @Title:CassandraQueryRepository.java
	 * @See: 构造函数 
	 * @param configMappings
	 * @date 2017年9月21日 上午10:51:53
	 */
	public NameQueriesRepository(BoEntity configMappings) {
//		Assert.notEmpty(configMappings, "configMappings is empty.");
		this.configMappings = configMappings;
		
	}
	/**
	 * 
	 * 说明：根据名称取得NameQuery
	 * @Title: getNameQuery
	 * @See: {@link #getNameQuery(String)}
	 * @author penght
	 * @param queryName 名称key
	 * @return NameQuery
	 * @date: 2017年9月21日 上午10:54:58
	 */
	public NameQuery getNameQuery(String queryName) {
		String queryKey = StringUtil.upperCase(queryName);
		return this.configMappings.getValue(queryKey);
	}
	
	@Override
	public boolean hasQuery(String queryName) {
		return this.configMappings.containsKey(queryName);
	}
	
	@Override
	public String getQuery(String queryName) {
		NameQuery nameQuery = getNameQuery(queryName);
		String query = nameQuery==null ? null : nameQuery.getQuery();
		if (logger.isDebugEnabled()) {
			logger.debug("queryName: {}, Query: {}", queryName , query);
		}
		return query;
	}
	
	public String getQuery(String queryName, BoEntity params) {
		String query = CqlBuilder.parse(this.getQuery(queryName), params);
		if (logger.isDebugEnabled()) {
			logger.debug("queryName: {}, Query: {}", queryName , query);
		}
		return query;
	}
}
